import { _decorator, Component, Label, Node, ProgressBar, Size, Sprite, UITransform } from 'cc';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { AppControll, EventName, skin_config } from '../manager/AppControll';
import { Loader } from '../tools/LoadManger';
import EvtManger from '../tools/EvtManger';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('SkinItem')
export class SkinItem extends Component {
    private mSpGun: Sprite = null;
    private mLbName: Label = null;
    private mNdLock: Node = null;
    private mNdUnLock: Node = null;
    private mLbEffect: Label = null;
    private mLbDropTier: Label = null;
    private mSpMiniGun: Sprite = null;
    private mPrProgress: ProgressBar = null
    private mLbFragmentCount: Label = null;
    private mConfig: skin_config = null;
    private mNdState: Node = null;

    protected onLoad(): void {
        this.mSpGun = this.node.getChildByName("skinImg").getComponent(Sprite);
        this.mLbName = this.node.getChildByName("name").getComponent(Label);
        this.mNdLock = this.node.getChildByName("stateEffect").getChildByName("lock");
        this.mNdUnLock = this.node.getChildByName("stateEffect").getChildByName("unlock");
        this.mLbDropTier = this.node.getChildByName("getAddress").getChildByName("lbJiesuo").getComponent(Label);
        this.mLbEffect = this.node.getChildByName("attr").getChildByName("attr").getComponent(Label);
        this.mSpMiniGun = this.node.getChildByName("debris").getChildByName("bg").getChildByName("spriteFrame").getComponent(Sprite);
        this.mPrProgress = this.node.getChildByName("debris").getChildByName("ProgressBar").getComponent(ProgressBar);
        this.mNdState = this.node.getChildByName("stateList");
        this.mLbFragmentCount = this.mPrProgress.node.getChildByName("progress").getComponent(Label);
        this.RegisteredEvent();
        this.InitView();
    }

    RegisteredEvent() {
        EvtManger.on(EventName.unlockSkin, this, this.InitView);
        EvtManger.on(EventName.updateUsingSkin, this, this.InitView);
    }

    protected onDisable(): void {
        EvtManger.off(EventName.unlockSkin, this, this.InitView);
        EvtManger.off(EventName.updateUsingSkin, this, this.InitView);
    }

    InitView() {
        Loader.LoadGunImage("mainGunSkin/" + this.mConfig.mainGunSkinRes, (spf) => {
            if (this.mSpGun) this.mSpGun.spriteFrame = spf;
            if (this.mSpMiniGun) this.mSpMiniGun.spriteFrame = spf;
        });
        this.mNdState.children.forEach(nd => {
            nd.active = false;
        })
        let count = 0;
        let total = 0;

        if (GameData.playerData.skinFragments.length == 0) {
            count = 0;
            total = this.mConfig.unlockNeedDebris
        }
        for (let i = 0; i < GameData.playerData.skinFragments.length; i++) {
            let skinfragment = GameData.playerData.skinFragments[i];
            if (skinfragment.skinId == this.mConfig.skinId) {
                count = skinfragment.count;
                total = this.mConfig.unlockNeedDebris;
                break;
            } else {
                count = 0;
                total = this.mConfig.unlockNeedDebris;
            }
        }
        if (!total) {
            count = 1;
            total = 1;
        }
        if (count > total) {
            count = total;
        }
        this.mLbName.string = this.mConfig.skinName;
        if (GameData.playerData.skin.indexOf(this.mConfig.skinId) != -1) {
            this.node.getChildByName("debris").active = false;
            this.mNdUnLock.active = true;
            this.mNdLock.active = false
            if (this.mConfig.skinId == GameData.playerData.curGunSkinId) {
                this.mNdState.getChildByName("using").active = true;
            } else {
                this.mNdState.getChildByName("canUse").active = true;
            }
        } else {
            this.mNdUnLock.active = false;
            this.mNdLock.active = true;
            if (count < total) {
                this.mNdState.getChildByName("lock").active = true;
            } else {
                this.mNdState.getChildByName("unlock").active = true;
            }
        }
        this.mLbEffect.string = this.mConfig.unlockValue;
        this.mLbDropTier.string = this.mConfig.debrisDrioValue;
        this.mPrProgress.progress = count / total;
        this.mLbFragmentCount.string = count + "/" + total;
    }

    InitSkinItemData(config: skin_config) {
        this.mConfig = config;
    }

    OnUnLock() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        AppControll.ShelvedTime = 0;
        GameData.playerData.skin.push(this.mConfig.skinId);
        EvtManger.do(EventName.unlockSkin, this.mConfig.skinId);
    }

    OnUpdateUsing() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        AppControll.ShelvedTime = 0;
        GameData.playerData.curGunSkinId = this.mConfig.skinId;
        EvtManger.do(EventName.updateUsingSkin, this.mConfig.skinId);
    }

}


